<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-health__articles}">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-select v-model="dataForm.articlesTypeId" clearable placeholder="请选择资讯类型">
            <el-option
              v-for="item in typeList"
              :key="item.id"
              :label="item.typeName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input v-model="dataForm.title" placeholder="请输入资讯标题" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="dataForm.searchKeywords" placeholder="请输入搜索关键词" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">{{ $t('query') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="goToReleasePage()">{{ $t('add') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="danger" @click="deleteHandle()">{{ $t('deleteBatch') }}</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle" style="width: 100%;">
        <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
        <el-table-column prop="title" label="资讯标题" header-align="center" align="center"></el-table-column>
        <el-table-column prop="articlesTypeName" label="资讯类型" header-align="center" align="center"></el-table-column>
        <el-table-column prop="searchKeywords" label="搜索关键词" header-align="center" align="center"></el-table-column>
        <el-table-column min-width="100" label="资讯封面" show-overflow-tooltip header-align="center" align="center">
          <template slot-scope="scope">
            <el-image style="width: 100px; height: 100px" fit="cover" :src="scope.row.imgUrl"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="上架状态" header-align="center" align="center">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0" @change="switchChange(scope.row)" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="首页展示" header-align="center" align="center">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.indexStatus" :active-value="1" :inactive-value="0" @change="switchIndexStatusChange(scope.row)" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="creatorName" label="创建者" header-align="center" align="center"></el-table-column>
        <el-table-column prop="createDate" label="创建时间" header-align="center" align="center"></el-table-column>
        <el-table-column prop="updaterName" label="更新者" header-align="center" align="center"></el-table-column>
        <el-table-column prop="updateDate" label="更新时间" header-align="center" align="center"></el-table-column>
        <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="goToReleasePage(scope.row.id)">{{ $t('update') }}</el-button>
            <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">{{ $t('delete') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="limit"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle">
      </el-pagination>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/health/articles/page',
        getDataListIsPage: true,
        exportURL: '/health/articles/export',
        deleteURL: '/health/articles',
        deleteIsBatch: true
      },
      dataForm: {
        id: '',
        title: '',
        searchKeywords: '',
        articlesTypeId: ''
      },
      typeList: []
    }
  },
  created () {
    this.listCategories()
    this.$router.afterEach((to,from) => {
      if (to.path === '/articles-list' && from.path === '/articles-release') {
        this.getDataList()
      }
    })
  },
  methods: {
    goToReleasePage (id) {
      this.$router.push({
        name: 'articles-release',
        params: { id: id },
      })
    },
    listCategories() {
      this.$http.get('/health/articlestype/list').then(({ data }) => {
        this.typeList = data.data
      })
    },
    switchChange(row) {
      this.$http.put('/health/articles', {
        id: row.id,
        status: row.status
      }).then(({ data }) => {
        if (res.code == 0) {
          this.$message({
            message: '上架成功',
            type: 'success'
          });
        } else {
          this.$message.error('上架失败');
        }
      })
    },
    switchIndexStatusChange(row) {
      this.$http.put('/health/articles', {
        id: row.id,
        indexStatus: row.indexStatus
      }).then(({ data }) => {
        if (res.code == 0) {
          this.$message({
            message: '展示成功',
            type: 'success'
          });
        } else {
          this.$message.error('展示失败');
        }
      })
    }
  }
}
</script>
